<template>
    <div id="new_home">
   		<ma-scroll :down="true" :getMore="getRecommend" :showMsg="false" :init="init">
            <dynamic v-for="(item,index) in recommendObj" :key="index" :isShow="true" :obj="item" @shield="shield" :index="index"></dynamic>
   		    <empty v-if="!recommendObj || recommendObj.length === 0" message="暂无推荐"></empty>
   		</ma-scroll>
    </div>
</template>
<script>
import dynamic from "components/dynamic/dynamic_home";
import getLocation from "services/get-location";
import defaultImg from "assets/flagImg.png";
import empty from "components/empty-page";
import recommendServices from "services/dynamicService/dynamic_recommended";
import shieldService from "services/dynamicService/shield";
//wx
import wx from "weixin-js-sdk";

export default {
  name:'commend_list',
  components: {
    dynamic,
    empty
  },
  data() {
    return {
      city: "",
      latitude: "43.821877",
      longitude: "125.239955",
      recommendObj: [],
      defaultImg: defaultImg,
      shieldIndex: -5,
      recommendIndex: -5,
      count: "5",
      init: false,
      recommendAllLoaded: false,
      isWxPublic: false
    };
  },
  created() {
    if (this.$.env === "wxPublic") {
      this.isWxPublic = true;
    }
    this.getRecommend()
    .then(res=>{
    	this.init = true
    })
  },
  mounted() {
    if(this.$.env === 'webOrWechat'){
      document.getElementById('wrapper').classList = 'wxCss'
    }
  },
  methods: {
    
    //计算距离
    getDistance(objs) {
    	console.log(objs)
      for (let i = 0; i < objs.length; i++) {
      //      let date = obj[i].publishDate;
      //      obj[i].publishDate = date.substring(5, 11);
        let distance = getLocation.getFlatternDistance(
          objs[i].shop.latitude,
          objs[i].shop.longitude,
          this.latitude,
          this.longitude
        );
        distance = Math.ceil(distance); // 转换后的距离值
        objs[i].distance =
          distance > 1000
            ?  (distance / 1000).toFixed(1) + "km"
            : distance < 100 ? " 100m " :  distance + "m";
      }
      return objs;
    },
    //获取推荐列表
    getRecommend(callback) {
      let vm = this;
      return new Promise(function(resolve, reject) {
        vm.recommendIndex += 5;
        recommendServices.bind(vm)(
          {
            type: "",
            index: vm.recommendIndex,
            count: vm.count
          },
          res => {
          	 vm.recommendObj = vm.recommendObj.concat(res)
          	 let objs = vm.getDistance(res);
          	 console.log(objs)
            resolve()
          },
          err => {},
          {
          	type:'commend'
          }
        );
      });
    },
    //屏蔽信息
    shield(params) {
      shieldService.bind(this)(
        {
          id: params.id
        },
        res => {
          this.recommendObj.splice(params.index, 1);
          this.$.toast("屏蔽信息成功");
        },
        err => {}
      );
    },
  }
};

</script>
<style>

#new_home .scroller{
	width:100vw;
}
#new_home #wrapper {
	top:100px;
}
.wxCss{
  top:50px!important
}
</style>
